package com.pengheng.mqtt.uav;


import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 无人机MQTT服务配置
 */
@Configuration
public class UAVMqttConfig {

    @Resource
    private UAVPushCallback uavPushCallback;

    @Resource
    private UAVMqttProperties uavMqttProperties;

    @Bean
    public MqttClient uavMqttClient() throws MqttException {
        MqttClient uavMqttClient = new MqttClient(uavMqttProperties.getHostUrl(), uavMqttProperties.getClientId(), new MemoryPersistence());
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        options.setUserName(uavMqttProperties.getUsername());
        options.setPassword(uavMqttProperties.getPassword().toCharArray());
        options.setConnectionTimeout(uavMqttProperties.getTimeout());
        options.setKeepAliveInterval(uavMqttProperties.getKeepalive());
        uavMqttClient.setCallback(uavPushCallback);
        uavMqttClient.connect(options);
        return uavMqttClient;
    }
}
